<layout:basic/>

<?php
$config = json_encode([
    [
        'id' => 'userlist',
        'title' => '用户列表',
        'width' => '5',
    ],
    [
        'id' => 'rolelist',
        'title' => '角色列表',
        'width' => '7',
    ],
]);
?>
<component:application.module.admin.library.component.Datatable config='$config'/>

<script>
    $(function () {
        isea.loader.use("datatables", function () {
            isea.datatables.solve(function () {

                var current_uid = 0;
                var dtUserlist = isea.datatables.create("#userlist", {
                    onselect: 1,//单选择或者多选择
                    config: {
                        columns: [
                            {
                                title: 'ID',
                                data: 'id',
                                width: "20%"
                            },
                            {
                                title: '用户名',
                                data: 'username',
                                width: "40%"
                            },
                            {
                                title: '邮箱',
                                data: 'email',
                                width: "40%"
                            }]
                    }
                }).onDraw(null, function (row) {
                    var data = dtUserlist.data(row);
                    console.log(data);
                    loadUserRoleList(current_uid = data.id);
                });

                var dtRolelist = isea.datatables.create("#rolelist", {
                    config: {
                        onselect: 0,
                        columns: [
                            {
                                title: 'ID',
                                data: 'id',
                                width: "20%"
                            },
                            {
                                title: '名称',
                                data: 'name',
                                width: "20%"
                            },
                            {
                                title: "状态",
                                data: function (row) {
                                    return (parseInt(row.role) > 0) ? "<i class='fa fa-star'></i>" : "";
                                },
                                width: "20%"
                            },
                            {
                                title: '操作',
                                data: function (row) {
                                    if (parseInt(row.role) > 0) {
                                        return "<button class='btn btn-danger btn-xs act-delete'>删除</button>";
                                    } else {
                                        return "<button class='btn btn-default btn-xs act-add'>添加</button>";
                                    }
                                },
                                width: "40%"
                            }
                        ]
                    }
                }).onDraw(function () {
                    $(".act-add").unbind("click").click(function () {
                        var tr = $(this).closest("tr");
                        var trdata = dtRolelist.data(tr);
                        $.get("__PUBLIC__/admin/auth/addUserRole", {
                            uid: current_uid,
                            rid: trdata.id
                        }, function (data) {
                            if (data.status) {
                                trdata.role = 1;
                                dtRolelist.update(trdata, tr);
                            } else {
                                isea.notify.show(data.message, true);
                            }
                        });
                    });
                    $(".act-delete").unbind("click").click(function () {
                        var tr = $(this).closest("tr");
                        var trdata = dtRolelist.data(tr);
                        $.get("__PUBLIC__/admin/auth/removeUserRole", {
                            uid: current_uid,
                            rid: trdata.id
                        }, function (data) {
                            if (data.status) {
                                trdata.role = 0;
                                dtRolelist.update(trdata, tr);
                            } else {
                                isea.notify.show(data.message, true);
                            }
                        });
                    });
                });

                function loadUserList() {
                    $.get("__PUBLIC__/admin/user/getlist", function (data) {
                        if (data.status) {
                            dtUserlist.load(data.data);
                        }
                    });
                }

                function loadUserRoleList(id) {
                    $.get('__PUBLIC__/admin/auth/getUserRole?uid=' + id, function (data) {
                        if (data.status) {
                            dtRolelist.load(data.data);
                        } else {
                            isea.notify.show(data.message, true);
                        }
                    });
                }
                loadUserList();
            });
        });
    });
</script>
